#!/usr/bin/env bash

set -e

if [ -n "$1" ]; then
  TARGET_ARCHITECTURE=$1
fi

RELEASE_DIR=${TARGET_ARCHITECTURE:+target/${TARGET_ARCHITECTURE}/release}
RELEASE_DIR=${RELEASE_DIR:-target/release}

if [ ! -d "${RELEASE_DIR}" ]; then
  echo "Release directory '${RELEASE_DIR}' does not exist."
  echo "Running 'cargo build --release${TARGET_ARCHITECTURE:+ --target ${TARGET_ARCHITECTURE}}' ..."
  cargo build --release ${TARGET_ARCHITECTURE:+--target "${TARGET_ARCHITECTURE}"}
fi

if [ -n "$2" ]; then
  DEVELOPER_ID=$2
fi

DEVELOPER_ID=${DEVELOPER_ID:-""}
TARGET="neovide"
EXTRAS_DIR="extra"
ASSETS_DIR="assets"
BUNDLE_DIR="${RELEASE_DIR}/bundle"

APP_NAME="Neovide.app"
APP_TEMPLATE="${EXTRAS_DIR}/osx/${APP_NAME}"
APP_DIR="${BUNDLE_DIR}/osx"
APP_BINARY="${RELEASE_DIR}/${TARGET}"
APP_BINARY_DIR="${APP_DIR}/${APP_NAME}/Contents/MacOS"
APP_EXTRAS_DIR="${APP_DIR}/${APP_NAME}/Contents/Resources"

DMG_NAME="Neovide${TARGET_ARCHITECTURE:+-${TARGET_ARCHITECTURE}}.dmg"
DMG_VOLNAME="Neovide"
DMG_FILESYSTEM="HFS+"
DMG_FORMAT="UDZO"
DMG_ICNS="${APP_EXTRAS_DIR}/Neovide.icns"
DMG_BACKGROUND="${ASSETS_DIR}/neovide-dmg-background.tiff"

if [ "$GENERATE_BUNDLE_APP" == "true" ]; then
  # Make Neovide.app
  mkdir -p "${APP_BINARY_DIR}"
  mkdir -p "${APP_EXTRAS_DIR}"
  cp -fRp "${APP_TEMPLATE}" "${APP_DIR}"
  cp -fp "${APP_BINARY}" "${APP_BINARY_DIR}"
  touch -r "${APP_BINARY}" "${APP_DIR}/${APP_NAME}"
  echo "Created '${APP_NAME}' in '${APP_DIR}'"

  # Useful to use `codesign` for local signing using local keychain certificates
  # In case our pipeline fails, we are still able to sign the app manually
  if [ -n "$DEVELOPER_ID" ]; then
    codesign --remove-signature "${APP_DIR}/${APP_NAME}"
    echo "Signing '${APP_BINARY}' with '${DEVELOPER_ID}'"
    codesign --force --options runtime --deep --sign "${DEVELOPER_ID}" "${APP_BINARY}"
    echo "Signed '${APP_BINARY}'"
    echo "Signing '${APP_BINARY_DIR}/${TARGET}' with '${DEVELOPER_ID}'"
    codesign --force --options runtime --deep --sign "${DEVELOPER_ID}" "${APP_BINARY_DIR}/${TARGET}"
    echo "Signed '${APP_BINARY_DIR}/${TARGET}'"
    echo "Signing '${APP_NAME}' in '${APP_DIR}' with '${DEVELOPER_ID}'"
    codesign --force --options runtime --deep --sign "${DEVELOPER_ID}" "${APP_DIR}/${APP_NAME}"
    echo "Signed '${APP_NAME}' in '${APP_DIR}'"
  fi
fi

if [ "$GENERATE_DMG" == "true" ]; then
  # Make Neovide.dmg
  create-dmg \
    --filesystem "${DMG_FILESYSTEM}" \
    --format "${DMG_FORMAT}" \
    --volname "${DMG_VOLNAME}" \
    --volicon "${DMG_ICNS}" \
    --background "${DMG_BACKGROUND}" \
    --window-size 650 470 \
    --icon-size 80 \
    --icon Neovide.app 240 320 \
    --app-drop-link 410 320 \
    "${APP_DIR}/${DMG_NAME}" \
    "${APP_DIR}/${APP_NAME}"

  # Useful to use `codesign` for local signing using local keychain certificates
  # In case our pipeline fails, we are still able to sign the app manually
  if [ -n "$DEVELOPER_ID" ]; then
    echo "Signing '${APP_DIR}/${DMG_NAME}' with '${DEVELOPER_ID}'"
    codesign --force --options runtime --deep --sign "${DEVELOPER_ID}" "${APP_DIR}/${DMG_NAME}"
    echo "Signed '${APP_DIR}/${DMG_NAME}'"
  fi
fi
